#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "CGXBitmap.h"
#include <QPushButton>
#include <QLabel>
#include <QCloseEvent>
#include <GalaxyIncludes.h>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include "mat2qtformatmethod.h"
#include "myimageview.h"

using namespace std;
using namespace cv;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

    class CSampleCaptureEventHandler : public ICaptureEventHandler
    {
    public:
        void DoOnImageCaptured(CImageDataPointer&objImageDataPointer, void *pUserParam)
        {
            /*try {
                MainWindow *pSingleCamDlg = (MainWindow*)pUserParam;
                pSingleCamDlg->m_pBitmap->Show(objImageDataPointer);
            } catch (CGalaxyException) {
            } catch (std::exception){
            }*///在bitmap中显示

            if (objImageDataPointer->GetStatus() == GX_FRAME_STATUS_SUCCESS)
            {
                MainWindow* CamUiPtr = (MainWindow*)pUserParam;
                //以下为连续采集部分的显示
                void* pRaw8Buffer = NULL;
                pRaw8Buffer = objImageDataPointer->ConvertToRaw8(GX_BIT_0_7);
                std::memcpy(CamUiPtr->CurImgMat.data, pRaw8Buffer, (objImageDataPointer->GetHeight()) * (objImageDataPointer->GetWidth()));
                //cv::flip(CamUiPtr->CurImgMat, CamUiPtr->CurImgMat, 0);//大恒的图像要进行翻转，但是此处似乎不需要翻转

                //调用自定义绘图函数进行绘制实时采集图像
                CamUiPtr->ShowCurImgInLabel(CamUiPtr->LabelShowCurImg, CamUiPtr->CurImgMat);
//                CamUiPtr->ShowCurImgInImgView(CamUiPtr->imageView, CamUiPtr->CurImgMat);

                //单帧采集
//                if (CamUiPtr->m_bCheckSaveBmp == true) {
//                    MakeMyDirectory(CamUiPtr->strFilePath);//创建文件夹
//                    std::string PathAndName = CamUiPtr->strFilePath + "\\" + CamUiPtr->strFileName;
//                    CamUiPtr->m_pBitmap->SaveBmp(objImageDataPointer, PathAndName);//保存单帧图像
//                    CamUiPtr->m_bCheckSaveBmp = false;

//                    //通过读取的方式画出单帧图像
//                    QString LoadImgName = QString::fromStdString(PathAndName);
//                    CamUiPtr->SingleImgPixMap.load(LoadImgName, nullptr, Qt::AutoColor);
//                    CamUiPtr->LabelSingleImg->setPixmap(CamUiPtr->SingleImgPixMap);

//                    //仅仅在激发采集的时候是有意义的
//                    CamUiPtr->TimeTakePhoto = clock();
//                    qDebug() << (double)(CamUiPtr->TimeTakePhoto - CamUiPtr->TimePushButton);
//                    CamUiPtr->TimePushButton = 0;
//                    CamUiPtr->TimeTakePhoto = 0;
//                }
            }//在OpenCV中用Label显示
        }
    };

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    CGXBitmap	*m_pBitmap;				//图像操作类

private slots:
    void on_pushButton_OpenCam_clicked();
    void on_pushButton_CloseCam_clicked();
    void on_pushButton_StartGrab_clicked();
    void on_pushButton_StopGrab_clicked();

    void on_pb_setExp_clicked();

    void on_pushButton_GetImg_clicked();

    void on_pushButton_SingleGrab_clicked();

private:
    Ui::MainWindow *ui;

    //用于界面更新的标识
    bool		m_bIsOpen;				//打开设备标识
    bool		m_bIsSnap;				//采集图像标识
    //大恒相机API
    CGXFeatureControlPointer			*m_pFeatureEventHandler;				//远端设备事件回调对象
    CSampleCaptureEventHandler			*m_pCaptureEventHandler;				//采集回调对象
    CGXDevicePointer					m_ObjDevicePtr;							//设备句柄
    CGXStreamPointer					m_ObjStreamPtr;							//设备流
    CGXFeatureControlPointer			m_ObjFeatureControlPtr;					//属性控制器
    CGXFeatureControlPointer			m_ObjStreamFeatureControlPtr;			//流层控制器

    //更新界面
    void UpdateUI();

    //Opencv参数
    Mat CurImgMat;
    void AllocateRoomForMatCurImgPtr();
    void ShowCurImgInLabel(QLabel *ptrLabelToShow, cv::Mat &CVMat);
    void ShowCurImgInImgView(MyImageView *imgView, cv::Mat &CVMat);

    int ImgWidth = 0;
    int ImgHeight = 0;

    //展示窗口指针
    QLabel* LabelShowCurImg;
//    QLabel* LabelSingleImg;
    MyImageView *imageView;
};
#endif // MAINWINDOW_H
